
package com.yuke.cloud.service.uac.controller.rpc;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.base.constant.GlobalConstant;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.PublicUtil;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.smc.dto.ShopInfoDto;
import com.yuke.cloud.service.api.uac.dto.*;
import com.yuke.cloud.service.api.uac.service.UacUserFeignApi;
import com.yuke.cloud.service.uac.dto.UserRegisterDto;
import com.yuke.cloud.service.uac.entity.AccSupplier;
import com.yuke.cloud.service.uac.entity.AccUser;
import com.yuke.cloud.service.uac.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * The class uac user feign client.
 *
 * @author
 */
@RestController
@RequestMapping
@Api(value = "API - UacUserFeignClient", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UacUserFeignClient extends BaseController implements UacUserFeignApi {

	@Resource
	private AccUserService accUserService;

	@Resource
	private AccUserTokenService accUserTokenService;

	@Resource
	private AccGroupService accGroupService;

	@Resource
	private AccUserGroupService accUserGroupService;

	@Resource
	private AccEmployeeGroupService accEmployeeGroupService;

	@Resource
	private AccCheckinLogService accCheckinLogService;

	@Resource
	private AccSupplierService accSupplierService;

	@Resource
	private AccDeliveryAddressService accDeliveryAddressService;

	@Override
	@ApiOperation(httpMethod = "GET", value = "根据仓库所在的组织ID,获取仓库管理员用户")
	public Wrapper<List<WarehouseAdminUserDto>> getWarehouseAdmins(@RequestParam("groupId") Long groupId) {
		List<WarehouseAdminUserDto> list = accUserService.findWarehousAdminsByGroupId(groupId, GlobalConstant.Sys.WAREHOUSE_ADMIN_ROLE_ID);

		return WrapMapper.ok(list);
	}

	@Override
	@ApiOperation(httpMethod = "GET", value = "根据用户ID获取用户信息")
	public Wrapper<UserInfoDto> getUserInfoByUserId(@ApiParam(name = "userId", value = "用户id") @PathVariable(value="userId") Long userId) {
		UserInfoDto userInfoDto = new UserInfoDto();
		AccUser accUser = accUserService.findUserInfoByUserId(userId);
		BeanUtils.copyProperties(accUser, userInfoDto);

		return WrapMapper.ok(userInfoDto);
	}

	@Override
	@ApiOperation(httpMethod = "POST", value = "根据用户ID数组获取用户信息")
	public Wrapper<List<UserInfoDto>> getUserInfoByUserIds(@ApiParam(name = "userIds", value = "用户id") @RequestBody List<Long> userIds) {
		List<UserInfoDto> userInfoDtos = new ArrayList<>();
		List<AccUser> accUsers = accUserService.findUserInfoByUserIds(userIds);

		for (AccUser accUser:accUsers) {
			UserInfoDto userInfoDto = new UserInfoDto();
			BeanUtils.copyProperties(accUser, userInfoDto);
			userInfoDtos.add(userInfoDto);
		}

		return WrapMapper.ok(userInfoDtos);
	}

	@Override
	@ApiOperation(httpMethod = "POST", value = "修改商家或供应商用户类型进行修改")
	public Wrapper modifyUserType(@ApiParam(name = "modifyUserTypeDto", value = "用户类型修改信息") @RequestBody ModifyUserTypeDto modifyUserTypeDto) {
		if (PublicUtil.isEmpty(modifyUserTypeDto)) {
			return WrapMapper.error("参数为空");
		}

		accUserService.modifyUserTypeToSellerOrSupplier(modifyUserTypeDto.getUserId(), modifyUserTypeDto.getType(), modifyUserTypeDto.getIsSet());
		return WrapMapper.ok();
	}

	/**
	 * 根据用户ID获取用户登录设备token.
	 *
	 * @param userId the 用户ID
	 *
	 * @return the UserLoginInfoDto
	 */
	@Override
	@ApiOperation(httpMethod = "GET", value = "根据用户ID获取用户登录设备token.")
	public Wrapper<List<UserLoginInfoDto>> getUserDeviceToken(@PathVariable("userId") Long userId) {
		if (PublicUtil.isEmpty(userId)) {
			return WrapMapper.error("参数为空");
		}

		List<UserLoginInfoDto> list = accUserTokenService.getUserDeviceToken(userId);

		return WrapMapper.ok(list);

	}

	/**
	 * 根据用户groupId获取用户group.
	 *
	 * @param groupId the 用户ID
	 *
	 * @return the UserLoginInfoDto
	 */
	@Override
	@ApiOperation(httpMethod = "POST", value = "根据用户groupId获取用户group.")
	public Wrapper<GroupInfoDto> getGroupInfoByGroupId(@RequestParam("groupId") Long groupId){
		System.out.println("根据用户groupId获取用户group"+groupId);
		GroupInfoDto groupInfoDto = accGroupService.getGroupInfoByGroupId(groupId);

		return WrapMapper.ok(groupInfoDto);

	}

	@Override
	public Wrapper updateUserGroup(@RequestParam("userId")Long userId, @RequestParam("groupId") Long groupId) {
		accUserGroupService.updateUserGroup(userId,groupId);
		return WrapMapper.ok();
	}

	@Override
	public Wrapper<Integer> updateAmountBalance(@RequestBody UserInfoDto userInfoDto) {
		AccUser accUser = new AccUser();
		accUser.setUserId(userInfoDto.getUserId());
		accUser.setAmountBalance(userInfoDto.getAmountBalance());
		int num = accUserService.updateUser(accUser);
		logger.info("updateAmountBalance返回值：{}",num);
		return WrapMapper.ok(num);
	}

	@Override
	public Wrapper getUserInfoDto(@RequestParam("loginName")String loginName) {
		List<UserInfoDto> userInfoDtos = new ArrayList<>();
		//AccUser byLoginName = accUserService.findByLoginName(loginName);
		List<AccUser> accUsers = accUserService.getFindByLoginName(loginName);
		if (accUsers != null&& accUsers.size()>0){

			for (AccUser accUser : accUsers) {
				UserInfoDto userInfoDto = new UserInfoDto();
				BeanUtils.copyProperties(accUser,userInfoDto);
				userInfoDtos.add(userInfoDto);
			}
		}
		return WrapMapper.ok(userInfoDtos);
	}

	@Override
	@ApiOperation(httpMethod = "POST", value = "根据用户ID数组获取用户信息")
	public Wrapper<PageInfo<UserInfoDto>> getUserPageByUserIds(@RequestBody JSONObject jsonObject) {
		Integer page = jsonObject.getInteger("page");
		Integer size = jsonObject.getInteger("size");
		List<Long> userIds = (List<Long>) jsonObject.get("userIds");
		Byte userType = jsonObject.getByte("userType");

		List<UserInfoDto> userInfoDtos = new ArrayList<>();

		if (userIds != null && userIds.size() > 0){
			PageHelper.startPage(page, size);
			List<AccUser> accUsers = accUserService.getUserPageByUserIds(userIds,userType);
			for (AccUser accUser:accUsers) {
				UserInfoDto userInfoDto = new UserInfoDto();
				//BeanUtils.copyProperties(accUser, userInfoDto);
				userInfoDto.setUserId(accUser.getUserId());
				userInfoDto.setHeadImgUri(accUser.getHeadImgUri());
				userInfoDto.setLoginName(accUser.getLoginName());
				userInfoDtos.add(userInfoDto);
			}
		}

		PageInfo pageInfo = new PageInfo(userInfoDtos);
		return WrapMapper.ok(pageInfo);
	}

	@Override
	@ApiOperation(httpMethod = "POST", value = "根据用户信息生成用用户账户、店铺")
	public Integer autoRegisterUserAndShop(@RequestBody JSONObject jsonObject) {
		String phoneNumber = jsonObject.get("phoneNumber").toString();
		String shopName = jsonObject.get("shopName").toString();
		Long wareHouseId = Long.parseLong(jsonObject.get("wareHouseId").toString());
		String address = jsonObject.get("address").toString();
		//填充用户信息
		UserRegisterDto userRegisterDto = new UserRegisterDto();
		userRegisterDto.setMobileNo(phoneNumber);
		userRegisterDto.setLoginPwd("123456");
		userRegisterDto.setConfirmPwd("123456");
		userRegisterDto.setRegisterSource("0");
		userRegisterDto.setAddress(address);

		Integer num = accUserService.autoRegisterUserAndShop(userRegisterDto, shopName, wareHouseId);
		return num;
	}

	@Override
	public Wrapper selectEmployInfo(@RequestBody AccEmployeeInfoDto accEmployeeInfoDto) {
		List<UserInfoDto> userInfoDtos = new ArrayList<>();
		List<AccEmployeeInfoDto> employeeUsers = accEmployeeGroupService.selectEmployInfo(accEmployeeInfoDto);
		if (employeeUsers != null&& employeeUsers.size()>0){
			for (AccEmployeeInfoDto accUser : employeeUsers) {
				UserInfoDto userInfoDto = new UserInfoDto();
				BeanUtils.copyProperties(accUser,userInfoDto);
				userInfoDtos.add(userInfoDto);
			}
		}
		return WrapMapper.ok(userInfoDtos);
	}

	@Override
	public List<Map> countWorkDay(@RequestBody Map map) {
		return accCheckinLogService.countWorkDay(map);
	}

	@Override
	public SupplierApiDto findSupplierById(@RequestParam("supplierId") Long supplierId) {
		SupplierApiDto supplierApiDto = new SupplierApiDto();
		AccSupplier accSupplier = accSupplierService.selectByKey(supplierId);
		BeanUtils.copyProperties(accSupplier,supplierApiDto);
		return supplierApiDto;
	}

	@Override
	public List<SupplierApiDto> findSupplierByIds(@RequestBody List<Long> Ids) {
		List<SupplierApiDto> list = accSupplierService.findSupplierByIds(Ids);
		return list;
	}

	@Override
	public Integer updateSupplierById(@RequestBody SupplierApiDto supplierApiDto) {
		AccSupplier accSupplier = accSupplierService.selectByKey(supplierApiDto.getSupplierId());
		accSupplier.setAmountPaid(supplierApiDto.getAmountPaid()+accSupplier.getAmountPaid());
		accSupplier.setAmountPayable(supplierApiDto.getAmountPayable()+accSupplier.getAmountPayable());
		int update = accSupplierService.update(accSupplier);
		return update;
	}


	@Override
	public Integer saveUserDeliveryAddress(@RequestBody ShopInfoDto shopInfoDto){
		return accDeliveryAddressService.saveUserDeliveryAddress(shopInfoDto);
	}
}
